//angular App
var app = angular.module('app', []);

app.controller('body',function($http,$scope,$rootScope){

    //content-main
    console.log('content-main');
    $('.class-one').on('click', function(e) {
        $(this).parent().find('.class-one').removeClass('active');
        $(this).addClass('active');
        var rankClass = $(this)[0].classList[1];
        console.log(rankClass);
    })

    //sidebar
    console.log('sidebar');
    $scope.gameList = [];
    $scope.areaServerObj = {};
    $scope.serverList = [];
    $scope.history = [];
    $rootScope.currentMain = '';
    $rootScope.kind = "";
    $rootScope.game = "";
    $rootScope.area = "";
    $rootScope.server = "";
    function isEmpty(obj) {
        for (var key in obj) {
            if (key) {
                return false;
            }
        }
        return true;
    }
    function changeMain(){
        if (!$rootScope.kind || !$rootScope.game || !$rootScope.area || !$rootScope.server) {
            return 0;
        }
        if ($rootScope.kind !== $rootScope.currentMain) {
            $('.content-main').html($('#'+$rootScope.kind+'-template').html());
        }
    }
    $scope.chooseKind = function ($event) {
        $($event.target).addClass('active');
        $rootScope.kind = $($event.target).attr('data-kind');
        funcToggle($('#game-open')[0]);
        changeMain();
    }
    function funcToggle(btn) {
        var toClose = $(btn).parent().parent().siblings();
        for (var i = 0; i < toClose.length; i++) {
            if ($(toClose[i]).hasClass('open')) {
                var toCloseDistance = $(toClose[i]).find('.son-menu').height();
                $(toClose[i]).find('.son-menu').css({ 'margin-top': "-" + toCloseDistance + "px", 'opacity': 0 });
                $(toClose[i]).removeClass('open');
            }
        }
        var target = $(btn).parent().parent().find('.son-menu');
        var closeDistance = target.height();
        var openDistance = target.parent().height();
        var state = btn.parentNode.parentNode.classList[1];
        target.parent().toggleClass('open');
        if (state === 'open') {
            target.css({ 'margin-top': "-" + closeDistance + "px", 'opacity': 0 });
        } else {
            target.css({ 'margin-top': openDistance + "px", 'opacity': 1 });
        }
    }
    $scope.toggleOpen = function($event) {
        var btn = $event.target;
        if (btn.parentNode.parentNode.id === "area-btn" && isEmpty($scope.areaServerObj)) {
            prompt('请先选择游戏');
            return 0;
        }
        if (btn.parentNode.parentNode.id === "game-btn" && !$scope.kind) {
            prompt('请先选择商品类别');
            return 0;
        }
        if (btn.parentNode.parentNode.id === "history-btn" && !$scope.history[0]) {
            prompt('暂时没有历史记录');
            return 0;
        }
        funcToggle(btn);
    };
    $scope.getAreaServer = function(game, $event) {
        $rootScope.game = game;
        var tarSonMenu = $('#area-btn #area');
        console.log(tarSonMenu.height());
        tarSonMenu.hide();
        var nowClick = $event.target;
        $(nowClick).parent().find('.son-item').removeClass('active');
        $(nowClick).addClass('active');
        console.log(game);
        changeMain();
        $http({
            url: hostUrl + '/core/server_lists/',
            method: 'get',
            params: { game: game }
        }).success(function(data) {
            $scope.areaServerObj = data;
            setTimeout(function() {
                tarSonMenu.css({ 'margin-top': "-" + tarSonMenu.height() + "px", 'opacity': 0 });
                tarSonMenu.show();
                funcToggle($('#area-open')[0]);
            }, 200);

        }).error(function(data) {
            prompt('获取区服列表失败');
        })
    };
    //获取游戏列表
    $http({
        url: hostUrl + '/core/game_lists/',
        method: 'get'
    }).success(function(data) {
        $scope.gameList = data['games'];
    }).error(function(data) {
        prompt('获取游戏列表失败');
    });
    $scope.setArea = function(area, $event) {
        $rootScope.area = area;
        var nowClick = $event.target;
        $(nowClick).parent().find('.son-item').removeClass('active');
        $(nowClick).addClass('active');
        $scope.serverList = $scope.areaServerObj[area];
        $('#server').show();
        changeMain();
    };
    $scope.setServer = function(server, $event) {
        $rootScope.server = server;
        var nowClick = $event.target;
        $(nowClick).parent().find('.son-item').removeClass('active');
        $(nowClick).addClass('active');
        $('#server').hide();
        changeMain();
    }


    //userInfo
    if (localStorage['token']) {
        var token = localStorage.getItem('token');
    } else {
        return 0;
    }
    if (notCheckArr.indexOf(checkPage()) < 0) {
        $http({
            url: hostUrl + '/user/info/',
            method: 'get',
            headers: {
                'Authorization': "Token " + token
            }
        }).success(function(data, status) {
            console.log(data);
            console.log(status);
            for (var key in data) {
                localStorage[key] = data[key];
            }
            $('#loginOrReg').hide();
            $('#user').show();
            
            $('#nickname').html(data['username']);
        }).error(function(data, status) {
            if (data['message'] === "invalid token" || status === 401) {
                prompt('错误的用户认证，请重新登录');
                clearToken();
            } else {
                prompt("获取用户信息失败");
            }
        })
    }
    $scope.logout = function() {
        clearToken();
        localStorage.QQL = false;
        isLogin = false;
        $('#loginOrReg').show();
        $('#user').hide();
    }
})